package cn.com.yinhex.as.util;

public class UrlCacheMonitor {

	public static String readCache(String url, String encoding, long millisecond) {
		return readCache(url, encoding, millisecond, false);
	}

	public static String readCache(String url, String encoding, long millisecond, boolean sync) {
		String md5 = EncryptUtil.getMD5(url);
		String tempfile = UrlCache.cachedir + "/url/" + md5.charAt(0) + "/" + md5.charAt(1) + "/" + md5 + ".html";
		Thread monitor = new UrlCache(url, tempfile, encoding, millisecond);
		if (sync) {
			monitor.run();
		} else {
			if (FileUtil.exists(tempfile)) {
				monitor.start();
			} else {
				monitor.run();
			}
		}
		return FileUtil.read(tempfile);
	}

	public static String download(String url, long millisecond) {
		String md5 = EncryptUtil.getMD5(url);
		String tempfile = UrlCache.cachedir + "/url/" + md5.charAt(0) + "/" + md5.charAt(1) + "/" + md5 + "";
		UrlCache monitor = new UrlCache(url, tempfile, "", millisecond);
		monitor.setDownload(true);
		if (FileUtil.exists(tempfile)) {
			monitor.start();
		} else {
			monitor.run();
		}
		return tempfile;
	}
}
